﻿using System;
using System.ComponentModel;

namespace DataConvertLib
{
	/// <summary>
	/// 数值线性转换类
	/// </summary>
	// Token: 0x0200000C RID: 12
	[Description("数值线性转换类")]
	public class MigrationLib
	{
		// Token: 0x06000059 RID: 89 RVA: 0x00003E78 File Offset: 0x00002078
		private static string GetErrorMsg(DataType type)
		{
			string result = string.Empty;
			switch (type)
			{
			case DataType.Byte:
				result = "设置范围：" + MigrationLib.ByteMin + "-" + MigrationLib.ByteMax;
				break;
			case DataType.Short:
				result = "设置范围：" + MigrationLib.ShortMin + "-" + MigrationLib.ShortMax;
				break;
			case DataType.UShort:
				result = "设置范围：" + MigrationLib.UShortMin + "-" + MigrationLib.UShortMax;
				break;
			case DataType.Int:
				result = "设置范围：" + MigrationLib.IntMin + "-" + MigrationLib.IntMax;
				break;
			case DataType.UInt:
				result = "设置范围：" + MigrationLib.UIntMin + "-" + MigrationLib.UIntMax;
				break;
			case DataType.Float:
				result = "设置范围：" + MigrationLib.FloatMin + "-" + MigrationLib.FloatMax;
				break;
			case DataType.Double:
				result = "设置范围：" + MigrationLib.DoubleMin + "-" + MigrationLib.DoubleMax;
				break;
			case DataType.Long:
				result = "设置范围：" + MigrationLib.LongMin + "-" + MigrationLib.LongMax;
				break;
			case DataType.ULong:
				result = "设置范围：" + MigrationLib.ULongMin + "-" + MigrationLib.ULongMax;
				break;
			default:
				result = "非有效值类型";
				break;
			}
			return result;
		}

		/// <summary>
		/// 获取线性转换结果
		/// </summary>
		/// <param name="value">原始值</param>
		/// <param name="scale">线性系数</param>
		/// <param name="offset">线性偏移</param>
		/// <returns>带操作结果的转换结果</returns>
		// Token: 0x0600005A RID: 90 RVA: 0x00003FD8 File Offset: 0x000021D8
		[Description("获取线性转换结果")]
		public static OperateResult<object> GetMigrationValue(object value, string scale, string offset)
		{
            if (scale == "1" && offset == "0")
            {
                return OperateResult.CreateSuccessResult(value);
            }

            try
            {
                string name = value.GetType().Name;
                object value2;
                switch (name.ToLower())
                {
                    case "byte":
                    case "int16":
                    case "uint16":
                    case "int32":
                    case "uint32":
                    case "single":
                        value2 = Convert.ToSingle((Convert.ToSingle(value) * Convert.ToSingle(scale) + Convert.ToSingle(offset)).ToString("N4"));
                        break;
                    case "int64":
                    case "uint64":
                    case "double":
                        value2 = Convert.ToDouble((Convert.ToDouble(value) * Convert.ToDouble(scale) + Convert.ToDouble(offset)).ToString("N4"));
                        break;
                    default:
                        value2 = value;
                        break;
                }

                return OperateResult.CreateSuccessResult(value2);
            }
            catch (Exception ex)
            {
                return new OperateResult<object>
                {
                    IsSuccess = false,
                    Message = "转换出错：" + ex.Message
                };
            }
        }

		/// <summary>
		/// 线性转换后的设定值
		/// </summary>
		/// <param name="set">设定值</param>
		/// <param name="type">数据类型</param>
		/// <param name="scale">线性系数</param>
		/// <param name="offset">线性偏移</param>
		/// <returns>带操作结果的转换结果</returns>
		// Token: 0x0600005B RID: 91 RVA: 0x0000422C File Offset: 0x0000242C
		[Description("线性转换后的设定值")]
		public static OperateResult<string> SetMigrationValue(string set, DataType type, string scale, string offset)
		{
			OperateResult<string> operateResult = new OperateResult<string>();
			bool flag = scale == "1" && offset == "0";
			if (flag)
			{
				try
				{
					switch (type)
					{
					case DataType.Byte:
						operateResult.Content = Convert.ToByte(set).ToString();
						break;
					case DataType.Short:
						operateResult.Content = Convert.ToInt16(set).ToString();
						break;
					case DataType.UShort:
						operateResult.Content = Convert.ToUInt16(set).ToString();
						break;
					case DataType.Int:
						operateResult.Content = Convert.ToInt32(set).ToString();
						break;
					case DataType.UInt:
						operateResult.Content = Convert.ToUInt32(set).ToString();
						break;
					case DataType.Float:
						operateResult.Content = Convert.ToSingle(set).ToString();
						break;
					case DataType.Double:
						operateResult.Content = Convert.ToDouble(set).ToString();
						break;
					case DataType.Long:
						operateResult.Content = Convert.ToInt64(set).ToString();
						break;
					case DataType.ULong:
						operateResult.Content = Convert.ToUInt64(set).ToString();
						break;
					default:
						operateResult.Content = set;
						break;
					}
					operateResult.IsSuccess = true;
					return operateResult;
				}
				catch (Exception)
				{
					operateResult.IsSuccess = false;
					operateResult.Message = "转换出错，" + MigrationLib.GetErrorMsg(type);
					return operateResult;
				}
			}
			OperateResult<string> result;
			try
			{
				switch (type)
				{
				case DataType.Byte:
					operateResult.Content = Convert.ToByte((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
					break;
				case DataType.Short:
					operateResult.Content = Convert.ToInt16((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
					break;
				case DataType.UShort:
					operateResult.Content = Convert.ToUInt16((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
					break;
				case DataType.Int:
					operateResult.Content = Convert.ToInt32((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
					break;
				case DataType.UInt:
					operateResult.Content = Convert.ToUInt32((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
					break;
				case DataType.Float:
					operateResult.Content = Convert.ToSingle((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
					break;
				case DataType.Double:
					operateResult.Content = Convert.ToDouble((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
					break;
				case DataType.Long:
					operateResult.Content = Convert.ToInt64((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
					break;
				case DataType.ULong:
					operateResult.Content = Convert.ToUInt64((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
					break;
				default:
					operateResult.Content = set;
					break;
				}
				operateResult.IsSuccess = true;
				result = operateResult;
			}
			catch (Exception)
			{
				operateResult.IsSuccess = false;
				operateResult.Message = "转换出错，" + MigrationLib.GetErrorMsg(type);
				result = operateResult;
			}
			return result;
		}

		// Token: 0x04000031 RID: 49
		private static string ByteMax = byte.MaxValue.ToString();

		// Token: 0x04000032 RID: 50
		private static string ByteMin = 0.ToString();

		// Token: 0x04000033 RID: 51
		private static string ShortMax = short.MaxValue.ToString();

		// Token: 0x04000034 RID: 52
		private static string ShortMin = 0.ToString();

		// Token: 0x04000035 RID: 53
		private static string UShortMax = ushort.MaxValue.ToString();

		// Token: 0x04000036 RID: 54
		private static string UShortMin = 0.ToString();

		// Token: 0x04000037 RID: 55
		private static string IntMax = int.MaxValue.ToString();

		// Token: 0x04000038 RID: 56
		private static string IntMin = int.MinValue.ToString();

		// Token: 0x04000039 RID: 57
		private static string UIntMax = uint.MaxValue.ToString();

		// Token: 0x0400003A RID: 58
		private static string UIntMin = 0U.ToString();

		// Token: 0x0400003B RID: 59
		private static string FloatMax = float.MaxValue.ToString();

		// Token: 0x0400003C RID: 60
		private static string FloatMin = float.MinValue.ToString();

		// Token: 0x0400003D RID: 61
		private static string LongMax = long.MaxValue.ToString();

		// Token: 0x0400003E RID: 62
		private static string LongMin = long.MinValue.ToString();

		// Token: 0x0400003F RID: 63
		private static string ULongMax = ulong.MaxValue.ToString();

		// Token: 0x04000040 RID: 64
		private static string ULongMin = 0UL.ToString();

		// Token: 0x04000041 RID: 65
		private static string DoubleMax = double.MaxValue.ToString();

		// Token: 0x04000042 RID: 66
		private static string DoubleMin = double.MinValue.ToString();
	}
}
